package yu.code.yurpc.server;

import io.vertx.core.Vertx;

/**
 * Vertx 实现的 HttpServer
 */
public class VertxHttpServer implements HttpServer{
    @Override
    public void start(int port) {
        //创建vertx实例
        Vertx vertx = Vertx.vertx();
        //创建HTTP服务器
        io.vertx.core.http.HttpServer httpServer = vertx.createHttpServer();
        //监听端口并处理请求
        httpServer.requestHandler(new HttpServerHandler());
        /*httpServer.requestHandler(request -> {
            //处理HTTP请求
            System.out.println("Received request:"+request.method()+" "+request.uri());
            //发送HTTP响应
            request.response()
                    .putHeader("content-type", "text/plain")
                    .end("Hello Vert.x!");
        });*/
        //启动HTTP服务器并监听指定端口
        httpServer.listen(port,result->{
            if(result.succeeded()){
                System.out.println("HTTP server started on port "+port);
            }else{
                System.out.println("Failed to start HTTP server");
            }
        });

    }
}
